package feri.rest;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Timer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.format.Time;
import android.text.method.DateTimeKeyListener;

import feri.baza.DBHelper;

public class JsonParser {
	
	public void parsajDogodek (String response, DBHelper dbh)
	{
    	String datum="";
    	String naslov;
    	String naziv="";
    	String opis;
    	double x;
    	double y;

    	try {
			JSONArray json = new JSONArray(response);
			
			//tw.setText(gf.toString());
			for (int i=0; i<json.length();i++)
        	{
        		JSONObject jo = json.getJSONObject(i);
        		datum= jo.getString("datum");
        		naslov=jo.getString("naslov");
        		naziv=jo.getString("naziv");
        		opis= jo.getString("opis");
        		x= jo.getDouble("x");
        		y= jo.getDouble("y");
        		dbh.dodajDogodek(naziv, opis, naslov, datum, x, y);
        	}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		date.setHours(date.getHours()+1);
		String niz =dateFormat.format(date);
		Time t = new Time();
		String updateDatum = niz.substring(0,10)+"%20"+niz.substring(11,19);
		dbh.vnesiDatumPosodobitve(3, updateDatum);
		
    	
    	
	}
	
	
	
	public void parsajlokacija (String response, DBHelper dbh)
	{
		int id;
    	String naslov;
    	String naziv="";
    	String opis;
    	double x;
    	double y;	
    	
    	try {
			JSONArray json = new JSONArray(response);
			
			//tw.setText(gf.toString());
			for (int i=0; i<json.length();i++)
        	{
				
        		JSONObject jo = json.getJSONObject(i);
        		
        		naslov=jo.getString("naslov");
        		naziv=jo.getString("naziv");
        		opis= jo.getString("opis");
        		x= jo.getDouble("x");
        		y= jo.getDouble("y");
        		
        		JSONObject kategorija = jo.getJSONObject("kategorijaIdkategorija");
        		id=kategorija.getInt("idkategorija");
        		dbh.dodajLokacijo(id, naziv,opis, naslov, x, y);
        	}
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Date date = new Date();
	date.setHours(date.getHours()+1);
		String niz =dateFormat.format(date);
		Time t = new Time();
		String datum = niz.substring(0,10)+"%20"+niz.substring(11,19);
		dbh.vnesiDatumPosodobitve(2, datum);

	}
	
	public void parsajKategorija (String response, DBHelper dbh)
	{
		int id=0;
		int nadkategorija=0;
    	String naziv="";

    	
    	try {
			JSONArray json = new JSONArray(response);
			
			
			
				for (int i=0; i<json.length();i++)
	        	{
	
	        		JSONObject jo = json.getJSONObject(i);
	        		id = jo.getInt("idkategorija");
	        		nadkategorija= jo.getInt("nadkategorija");
	        		naziv=jo.getString("naziv");
	        	dbh.dodajKategorijo(naziv, nadkategorija);  
	        
	        	}  
			
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			Date date = new Date();
		date.setHours(date.getHours()+1);
			String niz =dateFormat.format(date);
			Time t = new Time();
			String datum = niz.substring(0,10)+"%20"+niz.substring(11,19);
			dbh.vnesiDatumPosodobitve(1, datum);
				
			System.out.println("datum::"+datum); 
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}  



}
